//处理筛选项和按钮数据
export function formatFilter(data: any) {
  const { pageInfo } = data
  //格式化数据 将数据分为2部分
  const filterGroup = pageInfo.filter((item) => item.Type !== 'Button')
  const buttonGroup = pageInfo.filter((item) => item.Type == 'Button')
  return { filterGroup, buttonGroup }
}

//获取列表表头数据 取listInfo中的value 取fieldInfo中的name show
export function formatTableCol(data: any) {
  const { listInfo, fieldInfo } = data
  // 如果filedInfo为空，列表直接去listInfo
  const column = fieldInfo ?? fieldInfo
  const listCol =
    (!fieldInfo &&
      listInfo &&
      listInfo.length &&
      Object.keys(listInfo[0]).map((item) => {
        return {
          key: item
        }
      })) ||
    []
  //只显示Show=true的列数据
  // const column = arr.filter((item: any) => item.isShow == 'true')
  //有表头信息的情况下
  const headListInfo = data.headInfo ?? []
  const headColumn =
    (data.headFieldInfo &&
      Object.keys(data.headFieldInfo).map((item) => {
        return {
          key: item,
          ...data.headFieldInfo[item]
        }
      })) ||
    []
  return { listInfo, col: column, listCol, headListInfo, headCol: headColumn }
}

export function getColWidth(list, boxWidth) {
  const result = list.map((item) => {
    return {
      ...item,
      width: Math.floor((item.width / 100) * boxWidth)
    }
  })
  return result
}

export function setLastWidth(list, boxWidth) {
  //先把最后一个元素去掉再求和
  const len = list.length
  const arr = list.slice(0, len - 1)
  const sumWidth = arr.reduce((first, cur) => first + cur.width, 0)
  const lastWidth = boxWidth - sumWidth
  list[len - 1].width = lastWidth
}

export function setColWidth(list, boxWidth) {
  const result = list.map((item) => {
    return {
      ...item,
      width: Math.max(boxWidth / list.length, boxWidth * 0.1)
    }
  })
  return result
}

export function getQueryInfo() {
  const router = useRouter()
  const compName = router.currentRoute.value.name as string

  const key = compName + '_DATA'
  const dataStr = sessionStorage.getItem(key) as string
  const data = dataStr && JSON.parse(dataStr)
  return data
}
